package com.lineage.server.datatables;

import com.lineage.DatabaseFactory;
import com.lineage.server.utils.L1SpawnUtil;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

public final class SpawnRandomMobTable {
  private static Logger _log = Logger.getLogger(SpawnRandomMobTable.class.getName());
  
  private final Map<Integer, Data> _mobs;
  
  private static final Random _random = new Random();
  
  private static SpawnRandomMobTable _instance;
  
  public static SpawnRandomMobTable get() {
    if (_instance == null)
      _instance = new SpawnRandomMobTable(); 
    return _instance;
  }
  
  private SpawnRandomMobTable() {
    this._mobs = new HashMap<>();
    load();
  }
  
  private void load() {
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("SELECT * FROM spawnlist_random_mob");
      rs = pstm.executeQuery();
      while (rs.next()) {
        Data data = new Data();
        int id = rs.getInt("id");
        data.id = id;
        data.note = rs.getString("note");
        String[] temp = rs.getString("mapId").split(",");
        short[] i = new short[temp.length];
        int loop = 0;
        String[] array;
        int length = (array = temp).length;
        int j = 0;
        while (j < length) {
          String s = array[j];
          i[loop] = (short)Integer.parseInt(s);
          loop++;
          j++;
        } 
        data.mapId = i;
        data.mobId = rs.getInt("mobId");
        data.cont = rs.getInt("cont");
        data.timeSecondToDelete = rs.getInt("timeSecondToKill");
        data.isActive = rs.getBoolean("isActive");
        data.isbroad = rs.getBoolean("broad");
        this._mobs.put(Integer.valueOf(id), data);
      } 
    } catch (SQLException e) {
      e.printStackTrace();
      _log.log(Level.SEVERE, e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
    _log.info("载入随机出怪设置数量:" + this._mobs.size());
  }
  
  public void startRandomMob() {
    Iterator<Data> iterator = this._mobs.values().iterator();
    while (iterator.hasNext()) {
      Data data = iterator.next();
      if (data.isActive)
        L1SpawnUtil.spawnRandomMob(data.id); 
    } 
  }
  
  public short getRandomMapId(int RandomMobId) {
    Data data = this._mobs.get(Integer.valueOf(RandomMobId));
    if (data == null)
      return 0; 
    int length = ((Data)this._mobs.get(Integer.valueOf(RandomMobId))).mapId.length;
    int rand = _random.nextInt(length);
    return ((Data)this._mobs.get(Integer.valueOf(RandomMobId))).mapId[rand];
  }
  
  public int getRandomMapX(int mapId) {
    int startX = MapsTable.get().getStartX(mapId);
    int endX = MapsTable.get().getEndX(mapId);
    int rand = _random.nextInt(endX - startX);
    return startX + rand;
  }
  
  public int getRandomMapY(int mapId) {
    int startY = MapsTable.get().getStartY(mapId);
    int endY = MapsTable.get().getEndY(mapId);
    int rand = _random.nextInt(endY - startY);
    return startY + rand;
  }
  
  public String getName(int RandomMobId) {
    Data data = this._mobs.get(Integer.valueOf(RandomMobId));
    if (data == null)
      return ""; 
    return ((Data)this._mobs.get(Integer.valueOf(RandomMobId))).note;
  }
  
  public int getMobId(int RandomMobId) {
    Data data = this._mobs.get(Integer.valueOf(RandomMobId));
    if (data == null)
      return 0; 
    return ((Data)this._mobs.get(Integer.valueOf(RandomMobId))).mobId;
  }
  
  public int getCont(int RandomMobId) {
    Data data = this._mobs.get(Integer.valueOf(RandomMobId));
    if (data == null)
      return 0; 
    return ((Data)this._mobs.get(Integer.valueOf(RandomMobId))).cont;
  }
  
  public int getTimeSecondToDelete(int RandomMobId) {
    Data data = this._mobs.get(Integer.valueOf(RandomMobId));
    if (data == null)
      return 0; 
    return ((Data)this._mobs.get(Integer.valueOf(RandomMobId))).timeSecondToDelete;
  }
  
  public boolean isBroad(int RandomMobId) {
    Data data = this._mobs.get(Integer.valueOf(RandomMobId));
    return data.isbroad;
  }
  
  private class Data {
    private Data() {}
    
    public int id = 0;
    
    public String note = "";
    
    public int mobId = 0;
    
    public int cont = 0;
    
    public short[] mapId = new short[0];
    
    public int timeSecondToDelete = -1;
    
    public boolean isActive = false;
    
    public boolean isbroad = false;
  }
}
